package com.changgou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.changgou.canal.config.RabbitmqConfiguration;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
//监听数据库广告表
    @ListenPoint(schema = "changgou_business",table = "tb_ad")
    public void adUpdate(CanalEntry.EntryType entryType,CanalEntry.RowData rowData){
//        rowData.getBeforeColumnsList().forEach((c)-> System.out.println("更改前的数据"+c.getName()+"::"+c.getValue()));
//        rowData.getAfterColumnsList().forEach((c)-> System.out.println("更改后的数据"+c.getName()+"::"+c.getValue()));

        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if ("position".equals(column.getName())){
                System.out.println("发送最新数据:"+column.getValue());
                rabbitTemplate.convertAndSend("", RabbitmqConfiguration.AD_UPDATE_QUEUE,column.getValue());
            }
        }
    }

}
